home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / XML Utilities / Professional Programmer XSL IDE / Xselerator25.msi / Data.Cab / F17720_dates3.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2001-09-23  |  2.4 KB  |  57 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- ============================================================
  3. Example for sorting dates in the format d Month yyyy
  4. Use with: Dates3.xml
  5. Notes:-
  6. 1) The day portion of the date can be one or two digits
  7.    therefore it is necessary to pad this with zeroes so that
  8.    final sort select is a constant length numeric.
  9. 2) The month portion (specified as a month name) needs to
  10.    be converted to a numeric value.
  11. ================================================================= -->
  12. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  13. <xsl:output method="html" encoding="ISO-8859-1" indent="yes"/>
  14. <!-- define variable that is used to convert month names to numeric value -->
  15. <xsl:variable name="month-names"  select="'|January|February|March|April|May|June|July|August|September|October|November|December'"/>
  16. <xsl:variable name="month-values" select="'|01-----|02------|03---|04---|05-|06--|07--|08----|09-------|10-----|11------|12------'"/>
  17. <xsl:template match="/">
  18.     <html><body>
  19.     <table border="1">
  20.         <tr>
  21.             <th>Sorted Position</th>
  22.             <th>Sort value</th>
  23.             <th>Original date value</th>
  24.             <th>Original position</th>
  25.         </tr>
  26.         <xsl:for-each select="/root/item">
  27.             <!-- sort algorithm -->
  28.             <xsl:sort select="concat(
  29.                 substring-after(substring-after(@date,' '),' '),
  30.                 substring($month-values,string-length(substring-before($month-names,concat('|',substring-before(substring-after(@date,' '),' '))))+2,2),
  31.                 substring('00',1,2-string-length(substring-before(@date,' '))),substring-before(@date,' ')
  32.                 )" data-type="number"/>
  33.             <tr>
  34.                 <td>
  35.                     <xsl:value-of select="position()"/>
  36.                 </td>
  37.                 <td>
  38.                     <!-- the year portion -->
  39.                     <xsl:value-of select="substring-after(substring-after(@date,' '),' ')"/>
  40.                     <!-- the month portion -->
  41.                     <xsl:value-of select="substring($month-values,string-length(substring-before($month-names,concat('|',substring-before(substring-after(@date,' '),' '))))+2,2)"/>
  42.                     <!-- the day portion -->
  43.                     <xsl:value-of select="concat(substring('00',1,2-string-length(substring-before(@date,' '))),substring-before(@date,' '))"/>
  44.                 </td>
  45.                 <td>
  46.                     <xsl:value-of select="@date"/>
  47.                 </td>
  48.                 <td>
  49.                     <!-- show the order of the element in the original xml -->
  50.                     <xsl:value-of select="count(preceding-sibling::item)+1"/>
  51.                 </td>
  52.             </tr>
  53.         </xsl:for-each>
  54.     </table>
  55.     </body></html>
  56. </xsl:template>
  57. </xsl:stylesheet>